// 引入依赖
const koaAPP = require('koa')
const Router = require('koa-router')
const mongoose = require('mongoose')
// 用来配置post，接收post传来的数据
const Bodyparser = require('koa-bodyparser')
// 简便的实现登录注册功能
const passport = require('koa-passport')
// 引入passport
require("./config/passport")(passport);
// 引入托管静态资源
const static = require('koa-static')
const path = require('path')
// 实例化koa
const app = new koaAPP()
const router = new Router()
const bodyparser = new Bodyparser();

// 使用static托管静态资源
app.use(static(path.join(__dirname,'/public')))
// 配置路由， 使用中间件
app.use(bodyparser)//bodyparser初始化
app.use(router.routes(),router.allowedMethods())
app.use(passport.initialize()) //passport初始化
app.use(passport.session())

//config
const db = require('./config/env').dbURL

// 设置根路由
router.get('/',async (ctx) =>{
  ctx.body = {
      status:'200',
      msg:"接口连接成功"
  }
})

// 引入路由文件
const users = require('./routes/api/users')
const flowers = require('./routes/api/flowers')
const admins = require('./routes/api/admins')
const files = require('./routes/api/files')
const roles = require('./routes/api/roles')
const commodityOrders = require('./routes/api/CommodityOrders')
const Guidances = require('./routes/api/Guidances')
const Assess = require('./routes/api/Assess')
router.use('/users', users.routes(), users.allowedMethods());
router.use('/flowers', flowers.routes(), flowers.allowedMethods());
router.use('/admins', admins.routes(), admins.allowedMethods());
router.use('/files', files.routes(), files.allowedMethods());
router.use('/roles', roles.routes(), roles.allowedMethods());
router.use('/commodityOrders', commodityOrders.routes(), commodityOrders.allowedMethods());
router.use('/Guidances', Guidances.routes(), Guidances.allowedMethods());
router.use('/Assess', Assess.routes(), Assess.allowedMethods());
// 设置路由前缀
router.prefix('/api')
//连接数据库
//mongoose.connect('mongodb://数据库ip:端口号/数据库名'，{useMongoClient: true})
//  - 端口默认27017可省略
mongoose
  .connect(db, { useNewUrlParser: true })
  .then(() => {
    console.log('MongoDB connected...')
  })
  .catch((err) => {
    console.log(err)
  })
const port = 5000
app.listen(port,()=>{
    console.log(`Server is running at http://localhost:${port}`)
})